package org.redmagic.client;

import static com.google.common.base.Preconditions.checkNotNull;

import java.io.ByteArrayInputStream;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import com.google.common.collect.Lists;

public class RedMagicFileResolver {

    private final String resolvedConfigurationFile;

    public RedMagicFileResolver(String magicConfigFileName, String magicConfigSetupFileName) {
        this(magicConfigFileName, Lists.<String>newArrayList(magicConfigSetupFileName));
    }
    
    public RedMagicFileResolver(String magicConfigFileName, List<String> magicConfigSetupFileNames) {
        checkNotNull(magicConfigFileName);
        this.resolvedConfigurationFile = RedMagicConfigInitializer.prepareMagicConfig(magicConfigFileName, magicConfigSetupFileNames);
    }
    
    public String getResolvedConfig() {
        return resolvedConfigurationFile;
    }
    
    public Document getResolvedConfigAsDocument() {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder docBuilder = factory.newDocumentBuilder();
            Document configurationDocument = docBuilder.parse(new ByteArrayInputStream(resolvedConfigurationFile.getBytes()));
            return configurationDocument;            
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}
            
            
            